package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class EmpServiceImpl extends ServiceImpl<EmpMapper,Emp> implements EmpService {

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private EmpExprMapper empExprMapper;

    @Override
    public PageResult findEmpByPage(Integer page, Integer pageSize, String name, Integer gender, LocalDate begin, LocalDate end) {
        LambdaQueryWrapper<Emp>  wrapper = Wrappers.lambdaQuery();
        //定义查找什么
        wrapper.like( !name.equals(""),Emp::getName,name)
                .eq(gender!=null,Emp::getGender,gender)
                .between(begin!=null&&end!=null,Emp::getEntryDate,begin,end)
                .orderByDesc(Emp::getUpdateTime);
        //分页
        IPage<Emp> page1 = this.page(new Page<>(page,pageSize),wrapper);

        List<Emp> records = page1.getRecords();

        if(!CollectionUtils.isEmpty(records)){
            List<Integer> deptIds = records.stream().map(emp -> emp.getDeptId()).toList();

            deptIds=deptIds.stream().distinct().toList();
            log.info("deptIds:{}",deptIds);
            List<Dept> depts = deptMapper.selectByIds(deptIds);
            log.info("depts:{}",depts);
            records.forEach(emp -> {
                depts.stream().filter(dept -> dept.getId().equals(emp.getDeptId()))
                        .findFirst()
                        .ifPresent(dept->emp.setDeptName(dept.getName()));
            });
            log.info("records:{}",records);

        }
        return new PageResult<>(page1.getTotal(),records);
    }

    @Override
    @Transactional
    public void addEmp(Emp emp) {
        emp.setPassword("123456");
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());

        this.save(emp);

        List<EmpExpr> exprList = emp.getExprList();
        if(!CollectionUtils.isEmpty(exprList)){
            exprList.forEach(empExpr -> empExpr.setEmpId(emp.getId()));
            empExprMapper.insert(exprList);
        }
    }

    @Override
    public Emp findById(Integer id) {
        Emp emp = this.getById(id);

        LambdaQueryWrapper<EmpExpr> eq = Wrappers.<EmpExpr>lambdaQuery().eq(EmpExpr::getEmpId, emp.getId());
        List<EmpExpr> exprList = empExprMapper.selectList(eq);
        emp.setExprList(exprList);
        return emp;
    }

    @Override
    @Transactional
    public void updateEmp(Emp emp) {
        //设置修改时间
        emp.setUpdateTime(LocalDateTime.now());
        //调用BP修改
        this.updateById(emp);
        //查询出员工的工作经历
        LambdaQueryWrapper<EmpExpr> eq = Wrappers.<EmpExpr>lambdaQuery().eq(EmpExpr::getEmpId, emp.getId());
        //删除工作经历
        empExprMapper.delete(eq);
        //获取前端传过来的工作经历
        List<EmpExpr> exprList = emp.getExprList();

        if(!CollectionUtils.isEmpty(exprList)){
            //绑定id
            exprList.forEach(empExpr -> empExpr.setEmpId(emp.getId()));
            //将工作经历插入到数据库中
            empExprMapper.insert(exprList);
        }
    }

    @Override
    @Transactional
    public void deleteEmp(List<Integer> ids) {
        //删除员工
        this.removeByIds(ids);
        //查询出员工的工作经历
        LambdaQueryWrapper<EmpExpr> in = Wrappers.<EmpExpr>lambdaQuery().in(EmpExpr::getEmpId, ids);
        //删除工作经历
        empExprMapper.delete(in);
    }
}
